package com.example.blogsystem.demos.config;

import com.example.blogsystem.demos.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//登录的拦截器
@Slf4j
@Configuration
public class LoginInterceotor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("user_token");
        //log.info(token);
        Claims claims = JwtUtils.parseToken(token);

        if (claims == null){
            response.setStatus(401);
            //response.sendRedirect("/blog_login.html");
            return false;
        }
        return true;
    }
}
